<html>
<head>
    <meta charset="UTF-8">
    <title>智原资源管理系统</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="/layer/layer.js"></script>


</head>
<body>
<div id="app">
    <nav class="navbar navbar-inverse" style="border-radius: 0">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">智原资源管理系统</a>
            </div>
            <div id="navbar" class="collapse navbar-collapse">

                <ul class="nav navbar-nav">

                    <li class="active">
                        <a href="index.html">首页</a>
                    </li>
                    <li v-for="d in userResources" v-if="d.pid==0">
                        <a v-bind:href="d.url">{{d.name}}</a>
                    </li>


                </ul>
                <ul class="nav navbar-nav navbar-right hidden-sm">
                    <li><a>{{userName}}</a></li>
                    <li><a @click="logout()" href="#">退出</a></li>
                </ul>
            </div>

        </div>
    </nav>
    <div class="container">
        <div class="row">
            <h1>角色管理</h1>
            <a  type="button" class="btn btn-success" @click="addRole">
                添加
            </a>
        </div>

        <div class="row">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>名字</th>
                    <th>备注</th>
                    <th>创建时间</th>
                    <th>修改时间</th>
                    <th>
                        编辑
                    </th>
                </tr>
                </thead>
                <tbody>

                    <tr v-for="d in pageObj">
                        <td>{{d.id}}</td>
                        <td>{{d.name}}</td>
                        <td v-html="d.remark"></td>
                        <td>{{d.createTime}}</td>
                        <td>{{d.updateTime}}</td>
                        <td>
                            <a type="button" class="btn btn-info btn-sm" @click="roleAccredit(d.id)">
                                授权
                            </a>
                            <a  type="button" class="btn btn-info btn-sm" @click="updateRole(d.id)">
                                修改
                            </a>
                            <button  type="button" class="btn btn-danger btn-sm" @click="deleteId(d.id)">
                                删除
                            </button>
                        </td>
                    </tr>

                </tbody>
            </table>
        </div>
    </div>
    </div>
<script>
    new Vue({
        el: "#app",
        data: {
            userResources: [],
            userName: '',
            searchName: '',
            pageObj: {}

        },
        created() {
            this.getUserInfo();
            this.loadData();

        },
        methods: {
            loadData() {
                var vm = this;
                $.post("/role/list", {}, function (response) {
                    if (response.code == 200) {
                        vm.pageObj = response.data;
                    }
                })
            },

            getUserInfo() {
                var vm = this;
                $.post("/userInfo", {}, function (response) {
                    if (response.code == 200) {
                        vm.userName = response.data.name
                        vm.userResources = response.data.role.resourceList

                    } else {
                        location.href = "/login.html"
                    }
                })
            },
            logout() {
                $.post("/logout", {}, function (response) {
                    location.href = "/login.html";
                })
            },
            addRole() {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '添加角色',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'add.html',
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            deleteId(id) {
                var vm = this;
                var index = layer.confirm('您确定要删除吗？', {
                        btn: ['确定', '取消'] //按钮
                    },
                    function () {
                        $.post("/role/deleteId", {id: id}, function (response) {
                            if (response.code == 200) {
                                vm.loadData();
                            } else {
                                layer.msg(response.msg);
                            }
                            layer.close(index)
                        })
                    }, function () {

                    });
            },
            updateRole(id) {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '角色修改',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'update.html?id='+id,
                    end: function () {
                        vm.loadData();
                    }
                });
            },
            roleAccredit(id) {
                var vm = this;
                layer.open({
                    type: 2,
                    title: '角色授权',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['500px', '80%'],
                    content: 'accredit.html?id='+id,
                    end: function () {
                        vm.loadData();
                    }
                });
            }
        }
    });
</script>
</body>
</html>